<?php defined('BASEPATH') or exit('No direct script access allowed');

class Userdata
{

	public $name;
	var $cookie_path = '';
	var $cookie_domain = '';
	var $cookie_secure = FALSE;
	var $CI;
	var $data;
	var $store;

	function __construct($params = array())
	{
		log_message('debug', "Userdata Class Initialized");
		$this->CI = & get_instance();
		$this->CI->load->helper('cookie');
		$this->cookie_path = $this->CI->config->item('cookie_path');
		$this->cookie_domain = $this->CI->config->item('cookie_domain');
		$this->cookie_secure = $this->CI->config->item('cookie_secure');
		$name = 'userdata';
		if(isset($params['store']))
		{
			$this->store = $params['store'];
		}
		if(isset($params['name']))
		{
			$name = $params['name'];
		}
		$this->set_name($name);
		$this->read_data();
	}
	function set_name($name)
	{
		$this->name = $name;
		return $this;
	}
	function read_data()
	{
		$this->data = $this->CI->input->cookie($this->name);
		$this->data = $this->_unserialize($this->data);
		return $this;
	}
	function remove($item=null)
	{
		if(isset($this->data[$item]))
			unset($this->data[$item]);
		return $this;
	}
	function set($newdata = array(), $newval = '')
	{
		if (is_string($newdata))
		{
			$newdata = array($newdata => $newval);
		}

		if (count($newdata) > 0)
		{
			foreach ($newdata as $key => $val)
			{
				$this->data[$key] = $val;
			}
		}
		return $this;
	}
	function get($item, $default=null)
	{
		if(isset($_GET[$item]))
		{
			$this->data[$item] = $this->CI->input->get($item);
		}
		return $this->item($item, $default);
	}
	function post($item, $default=null)
	{
		if(isset($_POST[$item]))
		{
			$this->data[$item] = $this->CI->input->post($item);
		}
		return $this->item($item, $default);
	}
	function get_post($item, $default=null)
	{
		$this->data[$item] = $this->CI->get_post($item);
		return $this->item($item, $default);
	}
	function item($item=null, $default=null)
	{
		return ( ! isset($this->data[$item])) ? $default : $this->data[$item];
	}
	function getdata()
	{
		return $this->data;
	}
	function store($data=null)
	{
		if(is_null($data))
		{
			$data = $this->data;
		}
		if(!is_array($data))
		{
			$data = array();
		}
		$data = array_filter($data);
		$data = $this->_serialize($data);
		return setcookie(
			$this->name,
			$data,
			null,
			$this->cookie_path,
			$this->cookie_domain,
			$this->cookie_secure
		);
	}
	function clear()
	{
		// Kill the cookie
		return setcookie(
					$this->name,
					addslashes(serialize(array())),
					(time() - 31500000),
					$this->cookie_path,
					$this->cookie_domain,
					0
				);
	}
	function _serialize($data)
	{
		if (is_array($data))
		{
			foreach ($data as $key => $val)
			{
				if (is_string($val))
				{
					$data[$key] = str_replace('\\', '{{slash}}', $val);
				}
			}
		}
		else
		{
			if (is_string($data))
			{
				$data = str_replace('\\', '{{slash}}', $data);
			}
		}

		return serialize($data);
	}
	function _unserialize($data)
	{
		$data = @unserialize(strip_slashes($data));

		if (is_array($data))
		{
			foreach ($data as $key => $val)
			{
				if (is_string($val))
				{
					$data[$key] = str_replace('{{slash}}', '\\', $val);
				}
			}

			return $data;
		}

		return (is_string($data)) ? str_replace('{{slash}}', '\\', $data) : $data;
	}
	function __destruct()
	{
		if($this->store)
			$this->store();
	}
}
